package com.survivor.game;

import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import java.nio.FloatBuffer;
import java.util.ArrayList;
import java.util.List;

import javax.microedition.khronos.opengles.GL;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;

import com.survivor.activity.CoordHelper;
import com.survivor.activity.TextureLoader;
import com.survivor.linklines.R;

public class GameBackground {
	
	private float[] bgVerCoord, bgTexCoord, infoVerCoord;
	private FloatBuffer bgVerBuffer, bgTexBuffer, infoVerBuffer;
	
	private CoordHelper	ch;
	
	
	List<Bitmap> bmpArray = new ArrayList<Bitmap>();
	
	public static float bgRatio;
	
	float scrOfferX;
	
	private int[] textures = new int[2];
	
	
	public GameBackground(Context context) {
		// TODO Auto-generated constructor stub
		init(context);
	}
	
	private void init(Context context)
	{
		ch = new CoordHelper(context);
		bmpArray.add(BitmapFactory.decodeResource(context.getResources(), R.drawable.game_map_bg));
		bmpArray.add(BitmapFactory.decodeResource(context.getResources(), R.drawable.game_info_bg));
		bgRatio	= (float)bmpArray.get(0).getWidth() / (float)bmpArray.get(0).getHeight();
		calBgCoord();
		bgVerBuffer = makeFloatBuffer(bgVerCoord);
		bgTexBuffer = makeFloatBuffer(bgTexCoord);
		infoVerBuffer = makeFloatBuffer(infoVerCoord);
	}
	private void calBgCoord()
	{
		scrOfferX = (float)(ch.w - ch.h * bgRatio) / 4;
		
		bgVerCoord = new float[]
		             {
                       ch.scrX2OglX(scrOfferX), ch.scrY2OglY(ch.h), 0,
                       ch.scrX2OglX(scrOfferX + ch.h * bgRatio),ch.scrY2OglY(ch.h), 0,
                       ch.scrX2OglX(scrOfferX), ch.scrY2OglY(0), 0,
                       ch.scrX2OglX(scrOfferX + ch.h * bgRatio), ch.scrY2OglY(0), 0
		             };
		infoVerCoord = new float[]
		                         {
				ch.scrX2OglX(scrOfferX + ch.h * bgRatio + 1),ch.scrY2OglY(ch.h), 0,
				.9f, -1, 0,
				 ch.scrX2OglX(scrOfferX + ch.h * bgRatio + 1), ch.scrY2OglY(0), 0,
				 .9f, 1, 0
		                         };
		
		bgTexCoord = new float[]
		             {
				        0, 1,
				        1, 1,
				        0, 0,
				        1, 0
		             };
	}
	
	
	private FloatBuffer makeFloatBuffer(float[] src)
	{
		ByteBuffer bb = ByteBuffer.allocateDirect(src.length * 4);
		bb.order(ByteOrder.nativeOrder());
		FloatBuffer fb = bb.asFloatBuffer();
		fb.put(src);
		fb.position(0);
		return fb;
	}
	
	public void doDraw(GL10 gl)
	{
		gl.glLoadIdentity();
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, bgVerBuffer);
		gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, bgTexBuffer);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[0]);
		gl.glColor4f(1, 1, 1, 1f);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
		
		gl.glVertexPointer(3, GL10.GL_FLOAT, 0, infoVerBuffer);
		gl.glBindTexture(GL10.GL_TEXTURE_2D, textures[1]);
		gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);
	}
	
	public void loadTexture(GL10 gl)
	{
		TextureLoader.loadTextures(gl, bmpArray, textures);
	}
	
	

}
